package server.main.utils.mainutils;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;

public class ChangeXml {

    /**
     * 利用dom4j获取xml文件的doc对象
     * @param filename 文件路径
     * @return
     */
    public static Document load(String filename) {
        Document document = null;
        try {
            SAXReader saxReader = new SAXReader();
            document = saxReader.read(new File(filename)); // 读取XML文件,获得document对象
        } catch (Exception e) {
            e.printStackTrace();
        }
        return document;

    }


    public static boolean doc2XmlFile(Document document, String filename) {
        boolean flag = true;
        try {
            XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(filename), "UTF-8"));
            writer.write(document);
            writer.close();
        } catch (Exception e) {
            flag = false;
            e.printStackTrace();
        }
        System.out.println(flag);
        return flag;
    }

    /**
     * 更改pom.xmlz=中的dependencies
     * @param filename filename
     * @param groupId groupId
     * @param artifactId artifactId
     * @param version version（可为空）
     * @return
     */
    public static boolean changemoven(String filename,String groupId,String artifactId,String version){
        try {
            Document document = ChangeXml.load(filename);
            Element root = document.getRootElement();
            System.out.println(root);
            Element dependenciesele = root.element("dependencies");
            System.out.println(dependenciesele+" ");
            Element dependencyele = dependenciesele.addElement("dependency");
            System.out.println(dependencyele.getNamespace());
            Element groupIdele = dependencyele.addElement("groupId");
            groupIdele.addText(groupId);
            Element artifactIdele = dependencyele.addElement("artifactId");
            artifactIdele.addText(artifactId);
            if(version != null){
                Element versionid = dependencyele.addElement("version");
                versionid.setText(version);
            }
            doc2XmlFile(document,filename);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }


    /**
     * 用来更改web.xml文件中的路由信息
     * @param filename web.xml文件的路径
     * @param servlet_name servlet的名称
     * @param servlet_class servlet的实现类
     * @param url_pattern 路由信息
     * @return
     */
    public static boolean changeServlet(String filename,String servlet_name,String servlet_class,String url_pattern){

        try {
            Document document = ChangeXml.load(filename);
            Element root = document.getRootElement();
            Element servlet = root.addElement("servlet");
            Element servletname1 = servlet.addElement("servlet-name");
            servletname1.setText(servlet_name);
            Element servletclass = servlet.addElement("servlet-class");
            servletclass.setText(servlet_class);

            Element servletmapping = root.addElement("servlet-mapping");
            Element servletname2 = servletmapping.addElement("servlet-name");
            servletname2.setText(servlet_name);
            Element urlpattern = servletmapping.addElement("url-pattern");
            urlpattern.setText("/"+url_pattern);

            ChangeXml.doc2XmlFile(document,filename);
            return true;
        } catch (Exception e) {

            e.printStackTrace();
        }
        return false;

    }

    public static void main(String[] args) {
        ChangeXml.changemoven("D:\\code\\idea\\newbishe\\temp\\pom.xml","groupid","artifictid","version");

    }
}
